import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.HashSet;
public class ChatPanel extends JFrame {
    public Socket p ;
    public DatePacket d ;
    public HashSet<Youyou> qing;
  //  public HashSet<Youyou> x;
    OutputStream outputStream;
    public int strtoint(String i){
        int w = 0;
        char[] a = i.toCharArray();
        for(int j = 0;j<i.length() ;j++){


            if(a[j] < '0' ||a[j] > '9'){
                JOptionPane.showMessageDialog(this,"账号应全为数字");
                break;
            }else {
                w = (a[j] - 48 ) + w*10;
            }
        }
        return  w ;
    }
    public ChatPanel(Socket p, DatePacket d,OutputStream outputStream,HashSet<Youyou> qing) {
        this.qing = qing ;
        // 设置窗口的基本属性
        this.outputStream = outputStream;
        //this.x = xing;
        this.d = d;
        this.p = p;
        setTitle("个人主页");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        setLocationRelativeTo(null); // 窗口居中显示
// 创建北部面板，使用BorderLayout布局
        JPanel northernPanel = new JPanel(new BorderLayout());
        northernPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // 设置边框间距
// 创建昵称标签，并添加到北部面板的西侧
        JLabel nicknameLabel = new JLabel("昵称: " + d.name);
        nicknameLabel.setHorizontalAlignment(JLabel.LEFT); // 设置标签文本左对齐
        northernPanel.add(nicknameLabel, BorderLayout.WEST);
// 创建账号标签，并添加到北面板的中部
        JLabel accountLabel = new JLabel("        账号: " + d.account);
        accountLabel.setHorizontalAlignment(JLabel.LEFT); // 设置标签文本居中对齐
        northernPanel.add(accountLabel, BorderLayout.CENTER);
// 创建修改密码按钮，并添加到北部面板的东侧
        JButton modifyPasswordButton = new JButton("修改密码");
        modifyPasswordButton.setHorizontalAlignment(JButton.RIGHT); // 设置按钮文本右对齐
        northernPanel.add(modifyPasswordButton, BorderLayout.EAST);
// 将北部面板添加到主容器中，居北位置
        add(northernPanel, BorderLayout.NORTH);
// 创建西部面板，使用BoxLayout布局
        JPanel westernPanel = new JPanel();
        westernPanel.setLayout(new BoxLayout(westernPanel, BoxLayout.PAGE_AXIS)); // 设置布局为垂直方向
        westernPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); // 设置边框间距
// 创建创建房间按钮
        JButton createRoomButton = new JButton("创建房间");
// 创建销毁房间按钮
        JButton joinRoomButton = new JButton("加入房间");
// 将创建房间按钮添加到面板中
        westernPanel.add(createRoomButton);
// 在创建房间按钮和销毁房间按钮之间添加一个垂直间距
        westernPanel.add(Box.createVerticalStrut(10)); // 创建10像素的垂直间距
// 将销毁房间按钮添加到面板中
        westernPanel.add(joinRoomButton);
// 将西部面板添加到主容器中，居西位置
        add(westernPanel, BorderLayout.WEST);
// 创建中心面板，使用FlowLayout布局（用于展示其他信息）
        JPanel centerPanel = new JPanel(new FlowLayout());
        add(centerPanel, BorderLayout.CENTER);
        centerPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); // 设置边框间距
        // 创建一个新的JPanel并设置布局为FlowLayout，行数为1，列数为9
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 9, 8));
        buttonPanel.setPreferredSize(new Dimension(150, 150)); // 设置面板大小和位置固定
       // buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 10)); // 设置边框间距
        buttonPanel.setBorder(BorderFactory.createLineBorder(Color.WHITE)); // 设置边框颜色为黑色，使其看起来像一个方块
        // 设置buttonPanel的滚动条策略
        buttonPanel.setAutoscrolls(true);
        JScrollPane scrollPane = new JScrollPane(buttonPanel);
        //buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20)); // 设置组件间距和对齐方式
        // 添加按钮到面板中，每个按钮都是一个JButton对象
        int buttonCount = 0; // 用于跟踪按钮数量，以便在FlowLayout中定位它们
        for (Roompower i : d.rooms) {
            JButton wang = new JButton(i.roomname);
            wang.setPreferredSize(new Dimension(80, 60));
            wang.setFont(new Font("宋体", Font.PLAIN, 14));
            wang.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    for (Youyou ii : qing) {
                        if (ii.room == i.room) {
                            ii.chao.frame.setVisible(true);
                            break;
                        }
                    }
                }
            });
            buttonPanel.add(wang); // 将按钮添加到buttonPanel中，应用约束条件
            buttonCount++; // 更新按钮数量，以便跟踪位置
        }
        add(scrollPane, BorderLayout.CENTER); // 使用JScrollPane包装buttonPanel并添加到frame中
        modifyPasswordButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //showRoomInputDialog(); // 点击创建房间后，显示输入房间号的对话框
                int length = 0;
                String roomNumber = " ";
                while( length < 6 || length > 20){
                   roomNumber = JOptionPane.showInputDialog("请输入您的新密码(6-20位)");
                    length = roomNumber.length();
                }
                d.passward = roomNumber;
                d.direct = 6;
                try {
                    ObjectOutputStream   objectOutputStream = new ObjectOutputStream(outputStream);
                    objectOutputStream.writeObject(d);
                    objectOutputStream.flush();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        // 添加事件监听器
        createRoomButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int length = 0;
                String roomNumber = " ";
                while(true) {
                    if (length <= 0) {
                        roomNumber = JOptionPane.showInputDialog("请输入创建的房间名");
                        length = roomNumber.length();
                    }else if(length > 20){
                        roomNumber = JOptionPane.showInputDialog("用户名过长，请重新输入");
                        length = roomNumber.length();
                    }else{
                        break;
                    }
                }
                String Number1 = "";

                while(true) {
                    if (length < 6) {
                        Number1 = JOptionPane.showInputDialog("请输入创建的房间密码");
                        length = Number1.length();
                    }else if(length > 20){
                        Number1 = JOptionPane.showInputDialog("密码过长，请重新输入");
                        length = Number1.length();
                    }else{
                        break;
                    }
                }
                d.roomname = roomNumber;
                d.roomkey = Number1;
                d.direct = 1;
                try {
                    ObjectOutputStream   objectOutputStream = new ObjectOutputStream(outputStream);
                    objectOutputStream.writeObject(d);
                    objectOutputStream.flush();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        joinRoomButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int length = 0;
                String roomNumber = "";
                while(true) {
                    if (length == 0) {
                        roomNumber = JOptionPane.showInputDialog("请输入加入的房间号");
                        length = roomNumber.length();
                    }else{
                        break;
                    }
                }
                String Number1 = "";
                length = 0;
                while(true) {
                    if (length < 1) {
                        Number1 = JOptionPane.showInputDialog("请输入房间密码");
                        length = Number1.length();
                    }else if(length > 20){
                        Number1 = JOptionPane.showInputDialog("密码过长，请重新输入");
                        length = Number1.length();
                    }else{
                        break;
                    }
                }
                d.room = strtoint(roomNumber);
                d.roomkey = Number1;
                d.direct = 2;
                try {
                    ObjectOutputStream   objectOutputStream = new ObjectOutputStream(outputStream);
                    objectOutputStream.writeObject(d);
                    objectOutputStream.flush();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        setVisible(true);
    }
    public void end(){
        dispose();
}

}